package com.classtest.DemoExpend.controller;

import com.alibaba.fastjson.JSON;
import com.classtest.DemoExpend.bean.DbOrder;
import com.classtest.DemoExpend.bean.DbRelation;
import com.classtest.DemoExpend.bean.DbUser;
import com.classtest.DemoExpend.service.DbRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class HomeRelationController {

    @Autowired
    DbRelationService dbRelationService;

    //记录关注用户数量
    @RequestMapping("/homeRelation")
    public String homeRelation(HttpServletRequest request, DbRelation dbRelation){
        DbUser user = (DbUser) request.getSession().getAttribute("user");
        dbRelation.setUsId(user.getId());
        List<DbRelation> userRelationCount = dbRelationService.getRelationCount(JSON.toJSONString(dbRelation));
        int relationCount = 0; // 记录该用户下的关注数量
        Map<String,Object> relationResult = new HashMap<>();
        for (DbRelation relation:userRelationCount) {
            relationCount ++;
        }
        relationResult.put("relationCount",relationCount);
        return JSON.toJSONString(relationResult);
    }

}
